package com.advanced.collectionAndGeneric_1;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * DeleteArrayList--案例 2 里，已经使用 ArrayList 存储了学员的信息，
 * 现在实现删除学员信息，根据输 入的学号来进行删除(存入的学号是唯一的)，
 * 删除后将最新的学员信息输出。如果没有该学员 则提示信息。
 *
 * @author: Jovi
 * @createTime: 2017-05-17 14:43
 **/
public class DeleteArrayList {

    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Student s1 = new Student(1, "房祖名", 12);
        Student s2 = new Student(2, "张三", 13);
        Student s3 = new Student(3,"李四",13);
        Student s4 = new Student(4,"王五",14);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学员学号：");
        int id = sc.nextInt();
        int index = UpdateArrayList.getIndex(students, id);
        if (index >= 0) {
            students.remove(index);
            System.out.println("删除成功!");
            System.out.println("学员的信息如下：\n学号\t姓名\t年龄");
            for (Student stu : students) {
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge());
            }
        } else {
            System.out.println("对不起，没有此学员！");

        }
    }
}
